package com.chcit.share.security.util;

import com.chcit.share.security.ChcitUser;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 安全工具类
 *
 */
@UtilityClass
public class SecurityUtils {


	/**
	 * 获取用户
	 *
	 * @param authentication
	 * @return ChcitUser
	 * <p>
	 * 获取当前用户的全部信息 EnableChcitResourceServer true
	 * 获取当前用户的用户名 EnableChcitResourceServer false
	 */
	public ChcitUser getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof ChcitUser) {
			return (ChcitUser) principal;
		}
		return null;
	}

	/**
	 * 获取当前用名
	 *
	 * @return String
	 */
//	public String getUsername() {
//		Object principal = getAuthentication().getPrincipal();
//		if (principal instanceof String) {
//			return principal.toString();
//		}
//		return null;
//	}


//	public String getClientId() {
//		Authentication authentication = getAuthentication();
//		if (authentication instanceof OAuth2Authentication) {
//			OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
//			return auth2Authentication.getOAuth2Request().getClientId();
//		}
//		return null;
//	}

	/**
	 * 获取用户
	 */
	public ChcitUser getUser() {
		Authentication authentication = getAuthentication();
		if (authentication == null) {
			return null;
		}
		return getUser(authentication);
	}

	/**
	 * 获取用户角色信息
	 *
	 * @return 角色集合
	 */
	public List<Integer> getRoles() {
		Authentication authentication = getAuthentication();
		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

		List<Integer> roleIds = new ArrayList<>();
//		authorities.stream()
//				.filter(granted -> StringUtils.startsWith(granted.getAuthority(), SecurityConstants.ROLE))
//				.forEach(granted -> {
//					String id = StringUtils.removeStart(granted.getAuthority(), SecurityConstants.ROLE);
//					roleIds.add(Integer.parseInt(id));
//				});
		return roleIds;
	}

	/**
	 * 获取当前用户名
	 * @return
	 */
	public static String getUsername() {
		String username = null;
		Authentication authentication = getAuthentication();
		if(authentication != null) {
			Object principal = authentication.getPrincipal();
			if(principal != null && principal instanceof UserDetails) {
				username = ((UserDetails) principal).getUsername();
			}
		}
		return username;
	}

	/**
	 * 获取用户名
	 * @return
	 */
	public static String getUsername(Authentication authentication) {
		String username = null;
		if(authentication != null) {
			Object principal = authentication.getPrincipal();
			if(principal != null && principal instanceof UserDetails) {
				username = ((UserDetails) principal).getUsername();
			}
		}
		return username;
	}

	/**
	 * 获取当前登录信息
	 * @return
	 */
	public static Authentication getAuthentication() {
		if(SecurityContextHolder.getContext() == null) {
			return null;
		}
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication;
	}
}


